package cn.dengqiang.basic.service.impl;

import cn.dengqiang.basic.mapper.BaseMapper;
import cn.dengqiang.basic.query.BaseQuery;
import cn.dengqiang.basic.service.IBaseService;
import cn.dengqiang.basic.util.PageList;
import cn.dengqiang.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> mapper;

    @Override
    @Transactional
    public void add(T t) {
        mapper.save(t);
    }

    @Override
    @Transactional
    public void del(Long id) {
        mapper.remove(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    public List<T> getAll() {
        return mapper.loadAll();
    }

    @Override
    public T getById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //查询总数
        Integer total = mapper.queryCount(query);
        if (total==0) {
            return new PageList<>();
        }else {
            List<T> rows = mapper.queryData(query);
            return new PageList<>(total, rows);
        }
    }
}
